// controller for role management
const roleService = require('../services/roleService');
const asyncHandler = require('../middlewares/asyncHandler'); 

/**
 * 添加角色控制器
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
const addRoleController = async (req, res) => {
    try {
        const { name, description, permissions = [] } = req.body;
        const result = await roleService.addRole(name, description, permissions);
        
        res.json({
            code: 200,
            data: result,
            message: '角色创建成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

/**
 * 获取所有角色控制器
 */
const getAllRolesController = async (req, res) => {
    try {
        const result = await roleService.getAllRoles(req.query);
        res.json({
            code: 200,
            data: result,
            message: '获取角色列表成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

/**
 * 更新角色控制器
 */
const updateRoleController = async (req, res) => {
    try {
        const { id, name, description, permissions = [],active } = req.body;
        const result = await roleService.updateRole(id, name, description, permissions,active);

        res.json({
            code: 200,
            data: result,
            message: '角色更新成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

/**
 * 删除角色控制器
 */
const deleteRoleController = async (req, res) => {
    try {
        const { id } = req.params;
        await roleService.deleteRole(id);
        res.json({
            code: 200,
            data: null,
            message: '角色删除成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

/**
 * 获取角色权限控制器
 */
const getRolePermissionsController = async (req, res) => {
    try {
        const { id } = req.params;
        const result = await roleService.getPermissions(id);
        res.json({
            code: 200,
            data: result,
            message: '获取角色权限成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

/**
 * 分配权限给角色控制器
 */
const assignPermissionsController = async (req, res) => {
    try {
        const { id } = req.params;
        const result = await roleService.assignPermissions(id, req.body);
        res.json({
            code: 200,
            data: result,
            message: '权限分配成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}

const getAllPermissionsController = async (req, res) => {
    try {
        const result = await roleService.getAllPermissions();
        res.json({
            code: 200,
            data: result,
            message: '获取所有权限成功'
        });
    } catch (error) {
        res.status(400).json({
            code: 400,
            message: error
        });
    }
}
const assignMenusController = asyncHandler(async (req) => {
    return roleService.assignMenus(req.params.id,req.body)
})
const getRoleMenusController = asyncHandler(async (req) => {
    return roleService.getRoleMenus(req.params.id)
})



module.exports = {
    addRoleController,
    getAllRolesController,
    updateRoleController,
    deleteRoleController,
    getRolePermissionsController,
    assignPermissionsController,
    getAllPermissionsController,
    assignMenusController,
    getRoleMenusController,

}